/*
 * @lc app=leetcode.cn id=999 lang=typescript
 *
 * [999] 可以被一步捕获的棋子数
 */

// @lc code=start
function numRookCaptures(board: string[][]): number {
    let pos: number[] = [];
    board.some((row, y) => {
        return row.some((item, x) => {
            if (item === "R") {
                pos = [x, y];
                return true;
            }
        });
    });

    let count = 0;

    const findAround = (
        x: number,
        y: number,
        posTag?: "l" | "r" | "t" | "b"
    ) => {
        const val = board?.[y]?.[x];

        if (!val) return;

        if (posTag) {
            if (/[a-z]/.test(val)) {
                count++;
                return;
            }
            if (/[A-Z]/.test(val)) {
                return;
            }
        }
        switch (posTag) {
            case "l":
                findAround(x - 1, y, "l");
                break;
            case "r":
                findAround(x + 1, y, "r");
                break;
            case "b":
                findAround(x, y - 1, "b");
                break;
            case "t":
                findAround(x, y + 1, "t");
                break;
            default:
                findAround(x - 1, y, "l");
                findAround(x + 1, y, "r");
                findAround(x, y - 1, "b");
                findAround(x, y + 1, "t");
        }
    };
    findAround(pos[0], pos[1]);
    return count;
}
// @lc code=end
